package com.cennavi.minenavidemo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.cennavi.minenavidemo.R;
import com.minedata.minenavi.SDKInitializer;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.mapdal.BaseUrl;
import com.minedata.minenavi.mapdal.CoordType;
import com.minedata.minenavi.navi.ExpandView3;

public class LauncherActivity extends Activity {

    private SharedPreferences mSharedPreferences;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        mContext = this;
        initLocalConfig();

        findViewById(R.id.btn_init).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startMainActivity();
            }
        });

        findViewById(R.id.btn_set).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startSettingActivity();
            }
        });
    }

    private void startMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void startSettingActivity() {
        Intent intent = new Intent(this, SettingActivity.class);
        startActivity(intent);
    }

    /**
     * @param context
     * @param key
     * @param defaultValues
     * @return
     */
    private String getString(Context context, String key, String defaultValues) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getString(key, defaultValues);
    }

    private void initLocalConfig() {
        String routeingUrl = getString(mContext, "MineMap_routeingUrl", "");

        if (!TextUtils.isEmpty(routeingUrl)) {
            BaseUrl.RouteingUrl = routeingUrl;
        }

        String truckUrl = getString(mContext, "MineMap_truckUrl", "");

        if (!TextUtils.isEmpty(truckUrl)) {
            BaseUrl.TruckUrl = truckUrl;
        }

        String expandViewUrl = getString(mContext, "MineMap_expandViewUrl", "");
        String expandViewType = getString(mContext, "MineMap_expandViewType", "");

        if (!TextUtils.isEmpty(expandViewUrl) && !TextUtils.isEmpty(expandViewType)) {
            if (expandViewType.equals("0")) {
                SDKInitializer.setExpandViewUrl(ExpandView3.UrlType.roadnet, expandViewUrl);
            } else {
                SDKInitializer.setExpandViewUrl(ExpandView3.UrlType.realview, expandViewUrl);
            }
        }

        String routeTmcUrl = getString(mContext, "MineMap_routeTmcUrl", "");

        if (!TextUtils.isEmpty(routeTmcUrl)) {
            BaseUrl.RouteTmcUrl = routeTmcUrl;
        }

        String searchUrl = getString(mContext, "MineMap_searchUrl", "");

        if (!TextUtils.isEmpty(searchUrl)) {
            BaseUrl.SearchUrl = searchUrl;
        }

        String inverseUrl = getString(mContext, "MineMap_inverseUrl", "");

        if (!TextUtils.isEmpty(inverseUrl)) {
            BaseUrl.InverseUrl = inverseUrl;
        }

        String styleUrl = getString(mContext, "MineMap_styleUrl", "");
        String mapType = getString(mContext, "MineMap_mapType", "");

        if (!TextUtils.isEmpty(styleUrl) && !TextUtils.isEmpty(mapType)) {
            if (mapType.equals("0")) {
                SDKInitializer.setStyleUrl(MineMap.UrlType.basicMap, styleUrl);
            } else {
                SDKInitializer.setStyleUrl(MineMap.UrlType.satellite, styleUrl);
            }

        }

        String datastoreUrl = getString(mContext, "MineMap_datastoreUrl", "");

        if (!TextUtils.isEmpty(datastoreUrl)) {
            BaseUrl.DataStoreUrl = datastoreUrl;
        }

        String monitorUrl = getString(mContext, "MineMap_monitorUrl", "");

        if (!TextUtils.isEmpty(monitorUrl)) {
            BaseUrl.MonitorUrl = monitorUrl;
        }

        String serverHost = getString(mContext, "MineMap_serverHost", "");

        if (!TextUtils.isEmpty(serverHost)) {
            SDKInitializer.setServerHost(serverHost);
        }

        String serverScheme = getString(mContext, "MineMap_serverScheme", "");

        if (!TextUtils.isEmpty(serverScheme)) {
            SDKInitializer.setServerScheme(serverScheme);
        }

        String appKey = getString(mContext, "MineMap_appKey", "");

        if (!TextUtils.isEmpty(appKey)) {
            SDKInitializer.setApiKey(appKey);
        }

        String coordType = getString(mContext, "MineMap_coordType", "");

        if (!TextUtils.isEmpty(coordType)) {
            if (coordType.equals("0")) {
                SDKInitializer.setCoordType(CoordType.GCJ02);
            } else {
                SDKInitializer.setCoordType(CoordType.WGS84);
            }

        }

        String locationType = getString(mContext, "MineMap_locationType", "");

        if (!TextUtils.isEmpty(locationType)) {
            if (locationType.equals("0")) {
                SDKInitializer.setUseSimLocation(false);
            } else {
                SDKInitializer.setUseSimLocation(true);
            }
        }
    }
}
